-- UIMultiSelect
-- Created by panyl
-- 多项选择界面

UIMultiSelect = class("UIMultiSelect", function()
    return cc.Layer:create();
end);

function UIMultiSelect.create(options, title, dialog, para)
    return UIMultiSelect.new(options, title, dialog, para);
end

local MAX_OPTION_NUM    = 3;

-- 构造函数
function UIMultiSelect:ctor(options, title, dialog, para)
    self:setName("UIMultiSelect");
    local node = cc.CSLoader:createNode("layout/dungeon/special/Compass.csb");  -- 这里使用罗盘的界面，如果以后罗盘有需求调整需要用现在这个公共模块
    self:addChild(node);
    self.node = node;

    self.options = options;
    self.title   = title;
    self.dialog  = dialog;
    self.para    = para or {};

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 事件响应
function UIMultiSelect:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIMultiSelect 界面析构清理");
            EventMgr.removeAll("UIMultiSelect");
        end
    end);
end

-- 重绘
function UIMultiSelect:redraw()
    -- 设置标题
    local titleLabel = findChildByName(self.node, "CT/title");
    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(self.title);

    desc = self.dialog;

    -- 正文样式
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, desc);

    -- 绘制选项按钮
    local optionList = self:redrawOptions();

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    local nodeArr = table.append({ titleLabel, descLabel }, optionList);
    local offsetArr = { 25, 25, };

    local y_offset = self.para.y_offset or 0;

    bg:setPositionY(y_offset);

    for i = 1, sizeof(optionList) do
        table.insert(offsetArr, -10);
    end

    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL, nodeArr, offsetArr);

    -- 根据适配后的界面高度比例，对界面上的角标位置进行修正
    local bg1 = findChildByName(self.node, "CT/sub_bg_l");
    local bg2 = findChildByName(self.node, "CT/sub_bg_r");

    local fixY, deltaY = math.modf(AlignM.BG_SIZE_NORMAL.height * bg:getScaleY() / 2);
    fixY = fixY - 25 * bg:getScaleY();

    bg1:setPositionY(y_offset + fixY);
    bg2:setPositionY(y_offset + fixY);

    if self.para.icon then
        -- 背景的icon
        local iconImg = findChildByName(self.node, "CT/icon");

        iconImg:loadTexture(getItemIconPath(self.para.icon));

        local scale = 1.5;

        iconImg:setScale(scale);
        iconImg:setOpacity(80);
        iconImg:setPositionY(descLabel:getPositionY());
    else
        local iconImg = findChildByName(self.node, "CT/icon");
        iconImg:setVisible(false);
    end
end

-- 绘制选项按钮
function UIMultiSelect:redrawOptions()
    local optionList = {};

    local optionBtn = findChildByName(self.node, "CT/option1");
    -- 根据配置，添加选项按钮
    for index, option in ipairs(self.options) do
        local button;
        if index == 1 then
            -- 第一个直接使用原先的option1
            button = optionBtn;
        else
            -- 其他的需要自己复制一个按钮出来
            button = optionBtn:clone();

            optionBtn:getParent():addChild(button);
        end

        -- 加入选项列表
        table.insert(optionList, button);

        -- 设置选项的文本
        TextStyleM.setTextStyle(button, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);

        local text     = option["text"];
        local callback = option["callback"];
        local enable   = option["enable"];

        button:setTouchEnabled(enable);
        button:setBright(enable);

        button:setTitleText(text);

        -- 注册点击事件
        local function onButtonClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_return");
                callback();
            end
        end
        button:addTouchEventListener(onButtonClick);
    end

    return optionList;
end

-- 注册点击事件
function UIMultiSelect:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");

            UIMgr.getCurrentScene():removeFormByName("UIMultiSelect");
        end
    end
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
    AddBackEventHandler(self, btnBack, onBackClick);

    -- 黑色背景点击响应
    local bgNode = findChildByName(self.node, "bg/bgImg");
    bgNode:setTouchEnabled(true);
    bgNode:addTouchEventListener(onBackClick);
end

-- 适配
function UIMultiSelect:resize()
    local node = self.node;

    -- bg充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

